/**
 * Project Name:gc.sms.mvn
 * File Name:BaseDaoImpl.java
 * Package Name:com.clps.dev.sms.dao.impl
 * Date:2018-11-27 16:36:38
 * Copyright (c) 2018,http://www.clpsglobal.com/zh/home-cn/ All Rights Reserved.
 *
 */
package com.clps.dev.sms.common.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;

import com.clps.dev.sms.common.CommonData;
import com.clps.dev.sms.common.dao.IBaseDao;
import com.clps.dev.sms.util.order.GBKOrder;

/**
 * ClassName: BaseDaoImpl <br/>
 * Description:模板
 *
 * @author Jack.Huang
 * @version V1.0
 * @since JDK 1.8
   @date:2018-11-27 16:36:38
 */

public class BaseDaoImpl<T> extends HibernateDaoSupport implements IBaseDao<T> {

	private Class<?> clazz;
	private Logger logger = Logger.getLogger(Logger.class);

	/**
	 * Creates a new instance of BaseDaoImpl.
	 *
	 * @param clazz
	 */

	@Resource
	private SessionFactory sessionFactory;

	public Session getSession() {

		return sessionFactory.getCurrentSession();
	}

	public BaseDaoImpl(Class<?> clazz) {
		super();
		this.clazz = clazz;
	}
	@Autowired
	public void setMySessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	/**
	 * Description:添加数据
	 * @param T
	 * @return  void
	 * @see com.clps.dev.sms.common.dao.BaseDao#save(java.lang.Object)
	 */
	@Override
	public int save(T t) {

		if (this.getHibernateTemplate().save(t) != null) {
			logger.info("save success");
			return CommonData.success;
		} else {
			logger.info("delete failure");
			return CommonData.failure;
		}
	}

	/**
	 * Description：删除数据
	 * @param  id
	 * @return int
	 * @see com.clps.dev.sms.common.dao.BaseDao#delete(java.lang.Object)
	 */
	@Override
	public int deleteById(Serializable id) {
		T t = queryById(id);
		this.getSession().clear();
		if (null != t) {
			this.getHibernateTemplate().delete(t);
			T t2 = queryById(id);
			if (t2 == null) {
				logger.info("delete success");
				return CommonData.success;
			} else {
				logger.info("delete failure");
				return CommonData.failure;
			}
		} else {
			logger.info("no data");
			return CommonData.failure;
		}
	}

	/**
	 * Description:更新修改数据
	 * @param t,id
	 * @return int
	 * @see com.clps.dev.sms.common.dao.BaseDao#update(java.lang.Object)
	 */
	@Override
	public int update(T t, Serializable id) {
		T t2 = queryById(id);
		this.getSession().clear();
		if (null != t2) {
			this.getHibernateTemplate().update(t);
			T t3 = queryById(id);
			if (t3.equals(t)) {
				logger.info("update success");
				return CommonData.success;
			} else {
				logger.info("update failure");
				return CommonData.failure;
			}
		} else {
			logger.info("no data");
			return CommonData.failure;
		}
	}

	/**
	 * Description:通过id查询
	 * @param id
	 * @return T
	 * @see com.clps.dev.sms.common.dao.BaseDao#findById(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public T queryById(Serializable id) {

		return (T) this.getHibernateTemplate().get(clazz, id);
	}
	/**
	 * Description：查询全部
	 * @param
	 * @return List
	 * @see com.clps.dev.sms.common.dao.BaseDao#findALl()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> queryALl() {
		return (List<T>) this.getHibernateTemplate().find("from " + clazz.getSimpleName());
	}

	/**
	 *Description：统计个数
	 *@param detachedCriteria
	 *@return Integer
	 * @see com.clps.dev.sms.common.dao.BaseDao#findCount(org.hibernate.criterion.DetachedCriteria)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Integer findCount() {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz);
		detachedCriteria.setProjection(Projections.rowCount());
		List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
		if (list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}
	@SuppressWarnings("unchecked")
	@Override
	public Integer findCount(DetachedCriteria detachedCriteria) {
		detachedCriteria.setProjection(Projections.rowCount());
		List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
		if (list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	/**
	 * Description：分页查询
	 * @param detachedCriteria 离线查询
	 * @param begin  起始索引
	 * @param pageSize  每页记录数
	 * @return list
	 * @see com.clps.dev.sms.common.dao.BaseDao#findByPage(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> findByPage(Integer begin, Integer pageSize) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz);
		detachedCriteria.setProjection(null);
		return (List<T>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.common.dao.IBaseDao#findByPageOrderByName(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> findByPageOrderByName(Integer begin, Integer pageSize,
			String name) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz);
		detachedCriteria.setProjection(null);
		detachedCriteria.addOrder(GBKOrder.asc(name));
		return (List<T>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
	}

	/**
	 * 
	 * @see com.clps.dev.sms.common.dao.IBaseDao#findByPageOrderByTime(org.hibernate.criterion.DetachedCriteria, java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> findByPageOrderByTime(Integer begin, Integer pageSize,
			String createtime) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(clazz);
		detachedCriteria.setProjection(null);
		detachedCriteria.addOrder(Order.asc(createtime));
		return (List<T>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
	}
}
